package net.esj.basic.dao.ann.utils;

import java.io.InputStream;
import java.math.BigDecimal;
import java.security.Timestamp;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class TypeResolver {

	private static final Map<String,Class> typeMap = new HashMap();

	static {
		typeMap.put("float", Float.class);
		typeMap.put("float unsigned zerofill", Float.class);
		typeMap.put("double", Double.class);
		typeMap.put("decimal", BigDecimal.class);
		typeMap.put("money", BigDecimal.class);
		typeMap.put("currency", BigDecimal.class);
		typeMap.put("int", Integer.class);
		typeMap.put("uniqueidentifier", Integer.class);
		typeMap.put("nvarchar", String.class);
		typeMap.put("tinyint", Byte.class);
		typeMap.put("smallint", Short.class);
		typeMap.put("mediumint", Integer.class);
		typeMap.put("int2", Integer.class);
		typeMap.put("int4", Long.class);
		typeMap.put("int8", Long.class);
		typeMap.put("integer", Integer.class);
		typeMap.put("int identity",Integer.class);
		typeMap.put("bigint", Long.class);
		typeMap.put("numeric", Integer.class);
		typeMap.put("number", Integer.class);
		typeMap.put("serial", Integer.class);
		typeMap.put("int unsigned", Integer.class);
		typeMap.put("smallint", Short.class);
		typeMap.put("mediumint", Integer.class);
		typeMap.put("datetime",Date.class);
		typeMap.put("timestamp", Date.class);
		typeMap.put("timestamptz", Date.class);
		typeMap.put("typesystimestamp", Date.class);
		typeMap.put("smalldatetime", Date.class);
		typeMap.put("interval", Date.class);
		typeMap.put("time", Date.class);
		typeMap.put("date", Date.class);
		typeMap.put("smalldate", Date.class);
		typeMap.put("ntext",String.class);
		typeMap.put("text", String.class);
		typeMap.put("mediumtext", String.class);
		typeMap.put("mediumblob", InputStream.class);
		typeMap.put("longtext", String.class);
		typeMap.put("varchar", String.class);
		typeMap.put("varchar2", String.class);
		typeMap.put("char", String.class);
		typeMap.put("bpchar", String.class);
		typeMap.put("clob", String.class);
		typeMap.put("blob", String.class);
		typeMap.put("bit", Boolean.class);
		typeMap.put("bool", Boolean.class);
		typeMap.put("varbinary", String.class);
		typeMap.put("image", InputStream.class);
		typeMap.put("byte", InputStream.class);
	}
	
	public static Class resolveType (String type, boolean useDefault) {
		if (null == type) return null;
		Class s = typeMap.get(type.toLowerCase());
		if (null == s) {
			if (useDefault) return String.class;
			else {
				for(String key : typeMap.keySet()){
					if (type.toLowerCase().startsWith(key.toLowerCase()))
						return typeMap.get(key);
				}
				return null;
			}
		}
		else {
			return s;
		}
	}
}